<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class myCenterService extends MY_Service {  

    public $aid;
    
    public function __construct($data) { 
        parent::__construct();
        $this->aid = $data['aid'];
    }
    
    
    
    public function index(){
        
    }
 
    
/**
 * 
 * @param type $type 2未揭晓，1已揭晓
 */
   public function record_ajax($type,$page){

    $this->load->model('yygoods_record_model', 'goodRecord');
       switch ($type) {    
           case 1:
               $where ="gr.aid = ".$this->aid." and gc.status = 1";
               $list = $this->goodRecord->get_record(8,($page - 1)*8,false,$where);
               if(!empty($list)){
                   $list = $this->clCode($list);
               }
               break;  
           case 2:        
               $where ="gr.aid = ".$this->aid." and (gc.status = 2 or gc.status = 0)";  
               $list = $this->goodRecord->get_record(8,($page - 1)*8,false,$where); 
               break;
           default:
               show_404('参数错误');
               break;
       }
       
       return $list ? $list : '';
   }
   
   /**
     * 
     * @param type $type
     */
    public function getRecordCount($type) {
        $count = 1;
        $this->load->model('yygoods_record_model', 'record');
        switch ($type) {
            case 1:
                $where = "gr.aid = " . $this->aid . " and gc.status = 1";
                break;
            case 2:
                $where = "gr.aid = " . $this->aid . " and (gc.status = 2 or gc.status = 0)";
                break;
            default:
                return $count;
                break;
        }
        return $this->record->countListAll($where);
    }

    /**
    * 
    * @param type $gid
    */
    public function goods_ajax($page){    
        $this->load->model('yyorder_model', 'yyorder');
        $where = 'o.aid = '.$this->aid;
        $list = $this->yyorder->get_list(8,($page - 1)*8,false,$where);
        return $list ? $list : '';
    }
    
   
    public function goodsInfo($oid){
        $this->load->model('yyorder_model', 'yyorder');
        $where = 'o.id = '.$oid;
        $list = $this->yyorder->get_list(8,0,false,$where);
        return $list[0] ? $list[0] : '';
    }
    
    public function address($oid) {
        $this->load->model('yyorder_model', 'yyorder');
        $where = 'o.id = ' . $oid . ' and o.aid = '.$this->aid;
        $returnData = $this->yyorder->address(1, 0, false,$where);
        if(!empty($returnData)){
            return $returnData[0] ? $returnData[0] : '';
        }
        return '';
    }

    /**
     * 
     * @param type $orderId
     * @param type $addressId
     */
    public function addressSave($orderId,$addressId = '',$postData = ''){
        //更新地址表状态
        $this->load->model('yyaddress_model', 'yyaddress');
        $saveData = $this->checkAddress($postData);
        
        $isExist = $this->yyaddress->get_all(array('aid'=>$this->aid));
        if(empty($isExist)){
            $this->yyaddress->create($saveData);
        }else{
            $this->yyaddress->update($saveData,array('aid'=>$this->aid));
        }
        //更新订单表状态
        $this->load->model('yyorder_model', 'yyorder');   
       // $this->yyorder->updateStatus($orderId);
        $this->yyorder->update(array('status'=>1,'steptwotime'=>time(),'detail'=>$postData['detail'],'postcode'=>$postData['postcode'],'tel'=>$postData['tel'],'truename'=>$postData['truename'],'mobile'=>$postData['mobile']),'id = '.$orderId); 
        exit(json_encode(array('errno'=>0,'msg'=>'更新成功','url'=>  site_url('myCenter/goods/'.$orderId))));
    }
    
    
    
    
    
    public function checkAddress($postData){  
        if(empty($postData['truename'])){
            exit(json_encode(array('errno'=>1,'msg'=>'请填写真实姓名')));
        }
        if(empty($postData['mobile'])){
            exit(json_encode(array('errno'=>1,'msg'=>'请填写电话')));
        }
        if(empty($postData['detail'])){
            exit(json_encode(array('errno'=>1,'msg'=>'请填写地址')));
        }
        if(empty($this->aid)){
            exit(json_encode(array('errno'=>1,'msg'=>'超时，请重新登录')));
        }
        $postData['aid'] = $this->aid;   
        unset($postData['addressId']);
        unset($postData['orderId']);
        unset($postData['_scfs']);
        return $postData ? $postData : '';  
    }  
    
    /**
     * 处理中奖者代码
     */
    public function clCode($list){ 
        if(empty($list)){
            return '';
        }
        $this->load->model('yygoods_record_model', 'goodRecord');
       //字段冗余，暂时for循环查数据
        foreach ($list as &$v){
            $where = 'gr.aid = '.$v['gc_aid'].' and gr.gcid = '.$v['gc_id'];
            $data = $this->goodRecord->get_list(1,0,false,$where);
            if(!empty($data)){
                $v['prizeCode'] = $data[0]['codes'];
                $v['prizeMobile'] = $data[0]['mobile'];
                $v['prizeNickname'] = $data[0]['nickname'];
            }
        }
        return $list ? $list : '';
    }
    
    /**
     * 
     *  大转盘抽奖
     * @param type $type 1我参与 2我获得
     */
    public function drawPartakeAjax($type,$page){  
        $this->load->model('yyawards_record_model', 'record');
        switch ($type) {
            case 1:
                $where = 'gr.aid = '.$this->aid;
                $list = $this->record->drawPartake(8,($page - 1)*8,false,$where);
                break;
            case 2:  
                $where = 'r.aid = '.$this->aid.' and r.aid = gr.aid';  
                $list = $this->record->drawPartake(8,($page - 1)*8,false,$where);
                break;
            default:
                echo '参数错误';
                exit();
                break;
        }

        if(!empty($list)){
            $list = $this->getPrizeUser($list);
        }
       // p($list);
        return $list ? $list : '';
    }
    
    

    /**
     * 获取总数 1我参与 2我获得
     * @param type $type
     */
    public function getDrawPartakeCount($type) {
        $count = 1;
        switch ($type) {
            case 1:
                $this->load->model('yyawards_record_model', 'record');
                $where = 'gr.aid = ' . $this->aid;
                break;
            case 2:
                $this->load->model('yyawards_result_model', 'record');
                //$where = 'gc.aid = ' . $this->aid . ' and gc.aid = gr.aid';
                return $this->record->get_count_all(array('aid'=>$this->aid));
                break;
            default:
                return $count;
                break;
        }
        return $this->record->countListAll($where);
    }

    //获取中奖用户  
    public function getPrizeUser($list){
        // 获取用户信息
        $this->load->model('account_model', 'maccpay');
        foreach ($list as &$v){
            if(!empty($v['r_aid'])){
                //暂时查询
                $info = $this->maccpay->getinfo(array('id' => $v['r_aid']));
                
                $mobile = isset($info['mobile']) ? substr_replace($info['mobile'], '****', 3,4) : '';
                $v['prizeName'] = isset($info['nickname']) ? $info['nickname'] : $mobile;
                $v['prizeThumb'] = isset($info['thumb']) ? $info['thumb'] : '';
            }else{
                $v['prizeName'] = '';
                $v['prizeThumb'] = '';
            }
        }
        return $list;
    }
    
}
